package zhuzx.solution.ir;

import java.util.List;
import java.util.Map;

import org.zhuzx.util.GeneralUtil;
import org.zhuzx.util.JdbcUtil;
import org.zhuzx.util.ListDataUtil;

import zhuzx.generator.GuiUtil;

public class IrOrgProcessor {
	
	public static void main(String[] args) {
		String name = "余晓璟";
		String phone = "15972962866";
		findOrgInfo(name, phone);
		findClockRecOrgs(name, phone);
		
		additionalRecordingOrg(name, phone);
	}
	
	/**
	 * 找到该员工的组织全称和二三四级部门名称
	 */
	static List<String> findOrgInfo(String name, String phone) {
		Map<String, String> info = IrDataHelper.getAddressBookInfo(name, phone);
		if (info == null) {
			return null;
		}
		String fullOrgName = info.get("QYWX_ORG_NAME");
		String sql = "select ORG_LEVEL_TWO_NAME, ORG_LEVEL_THERE_NAME, "
				+ "ORG_LEVEL_FOUR_NAME, ORG_NAME from\n"
				+ "OA.IR_QYWX_ORG where org_name = '" + fullOrgName + "'";
		List<Map<String,String>> list = IrDataHelper.query("根据组织全名查org表", sql, fullOrgName);
		if (list.size() == 0) {
			GuiUtil.printOnExit("IR_QYWX_ORG表中找不到此组织全称：" + fullOrgName);
			return null;
		}
		List<String> orgs = get234OrgNames(info);
		System.out.println(name + phone + "的qywx_org_name：" + fullOrgName);
		System.out.println("address表中存储的org_number从IR_ORG_MAPPING查到部门名称：" + orgs);
		orgs.add(0, fullOrgName);
		return orgs;
	}
	
	/**
	 * 根据参数中的三个org_number找到对应的部门名称
	 */
	static List<String> get234OrgNames(Map<String,String> info) {
		String orgNo2 = info.get("ORG_NUMBER2");
		String orgNo3 = info.get("ORG_NUMBER3");
		String orgNo4 = info.get("ORG_NUMBER4");
		String sql = "select org_name from OA.IR_ORG_MAPPING where org_no in('%s','%s','%s')"
				+ " order by org_type";
		sql = String.format(sql, orgNo2, orgNo3, orgNo4);
		List<Map<String,String>> list = JdbcUtil.executeQuery(sql);
		String key = orgNo2 + "," + orgNo3 + "," + orgNo4;
		System.out.println(key + "查询到" + list.size() + "条IR_ORG_MAPPING记录");
		if (list.size() == 0) {
			return null;
		}
		List<String> orgs = ListDataUtil.getValues(list, "ORG_NAME");
		return orgs;
	}
	
	/**
	 * 找到该员工最近的打卡记录中各级部门的名称
	 */
	static void findClockRecOrgs(String name, String phone) {
		String sql = "select org_number2, org_number3, org_number4,"
				+ "\ncreate_date, update_date, company_name"
				+ " from OA.IR_CLOCK_IN_INFO\n"
				+ " where 1=1" + IrDataHelper.createNamePhoneExp(name, phone)
				+ " order by create_date desc";
		sql = JdbcUtil.wrapInLimit(sql, 0, 5, JdbcUtil.getDbConn(null));
		IrDataHelper.query("findClockRecOrgs", sql, name+phone).forEach(rec -> {
			boolean flag = rec.get("CREATE_DATE").equals(rec.get("UPDATE_DATE"));
			System.out.println(get234OrgNames(rec) + (flag ? "" : "\t有更新"));
		});
	}

	/**
	 * 插入该员工所属的新组织到org表
	 */
	static void additionalRecordingOrg(String name, String phone) {
		Map<String,String> info = IrDataHelper.getAddressBookInfo(name, phone);
		String fullOrgName = info.get("QYWX_ORG_NAME");
		boolean isInserted = insertOrg(fullOrgName, info.get("ORG_NUMBER2"),
				info.get("ORG_NUMBER3"), info.get("ORG_NUMBER4"));
		IrDataHelper.showResult(isInserted ? "已补录对应组织：" + fullOrgName :
				"已存在完整组织名，并有" + findOrgInfo(name, phone).size() + "级部门。");
	}
	
	/**
	 * 插入IR_QYWX_ORG表，插入的前提是必须已存在对应的三级部门。
	 */
	static boolean insertOrg(String fullOrgName,
			String orgNo2, String orgNo3, String orgNo4) {
		String sql = "select ORG_LEVEL_THERE_NAME, ORG_LEVEL_FOUR_NAME, ORG_NAME from\n"
				+ "OA.IR_QYWX_ORG where org_name = '" + fullOrgName + "'";
		List<Map<String,String>> list = IrDataHelper.query("根据组织全名查org表", sql, fullOrgName);
		if (list.size() > 0) {
			System.out.println("已存在：" + fullOrgName);
			return false;
		}
		sql = "select org_name from OA.IR_ORG_MAPPING where org_no in('%s','%s','%s')"
				+ " order by org_type";
		sql = String.format(sql, orgNo2, orgNo3, orgNo4);
		String key = orgNo2 + "," + orgNo3 + "," + orgNo4 + "；" + fullOrgName;
		list = IrDataHelper.query("根据各级org_number查对应的部门名称", sql, key);
		if (list.size() < 2) {
			String msg = GeneralUtil.isBlank(orgNo3) ? fullOrgName + "的address记录中无第三级部门" :
				"IR_ORG_MAPPING中找不到" + orgNo3 + "对应的记录：" + key;
			GuiUtil.printOnExit("【警告】" + msg);
		}
		String[][] params = {
			{"id", IrDataHelper.createId("OA.IR_QYWX_ORG")},
			{"org_name", fullOrgName},
			{"ORG_LEVEL_TWO_NAME", list.get(0).get("ORG_NAME")},
			{"ORG_LEVEL_THERE_NAME", list.get(1).get("ORG_NAME")},
			{"ORG_LEVEL_FOUR_NAME", list.size()>2 ? list.get(2).get("ORG_NAME") : "99999"},
			{"create_user", "zhuzexiang"},
			{"create_date", GeneralUtil.getDateTime()}
		};
		JdbcUtil.executeUpdate(JdbcUtil.createInsertSql(
				"OA.IR_QYWX_ORG", GeneralUtil.createMapping(params)));
		GuiUtil.printOnExit("已插入：" + params);
		return true;
	}
	
}